/**
 *PopupMenu.java
 *@anthor Daniel Liu
 *2006-4-28 3:13:16
 **/
package lex.explorer.file;

import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.KeyEvent;

import javax.swing.*;

import org.lex.explorer.file.DeleteAction;
import org.lex.explorer.file.DesktopAction;
import org.lex.explorer.file.FileExplorer;
import org.lex.explorer.file.NewFileAction;
import org.lex.explorer.file.NewFolderAction;
import org.lex.explorer.file.PropertiesAction;
import org.lex.explorer.file.RenameAction;

class TablePopupMenu extends JPopupMenu {
	private Action newFolder;
	private Action newFile;
	private Action open;
	private Action edit;
	private Action print;
	private Action rename;
	private Action delete;
	private Action properties;
	private Action[] zeroSelection;
	private Action[] singleSelection;
	private Action[] multiSelection;
	private Action[] allActions;
	// ========================================
	private FileExplorer fileExplorer = null;

	public TablePopupMenu(FileExplorer fileExplorer) {
		super();
		this.fileExplorer = fileExplorer;
		initialize();
	}

	public void updateUI()
	{
		super.updateUI();
		MenuElement[] menus = this.getSubElements();
		for( MenuElement menu : menus )
			((JMenuItem) menu).updateUI();
	}

	private void initialize() {
		// create actions
		this.createActions();
		// add actions to menu
		JMenu newMenu = new JMenu("New");
		newMenu.add(this.newFolder);
		newMenu.add(this.newFile);
		this.add(newMenu);
		this.addSeparator();
		this.add(open);
		this.add(print);
		this.addSeparator();
		this.add(edit);
		this.add(rename);
		this.add(delete);
		this.addSeparator();
		this.add(properties);
		// config table key binding
		JTable table = fileExplorer;
		InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		ActionMap am = table.getActionMap();
		for (int i = 0; i < this.allActions.length; i++) {
			Action a = this.allActions[i];
			KeyStroke ks = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
			if (null == ks)
				continue;
			else {
				String name = (String) a.getValue(Action.NAME);
				am.put(name, a);
				im.put(ks, name);
			}
		}
		am.put("delete", delete);
		im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
	}

	private void createActions() {
		newFolder = new NewFolderAction(fileExplorer);
		newFile = new NewFileAction(fileExplorer);
		open = new DesktopAction(fileExplorer, Desktop.Action.OPEN);
		edit = new DesktopAction(fileExplorer, Desktop.Action.EDIT);
		print = new DesktopAction(fileExplorer, Desktop.Action.PRINT);
		rename = new RenameAction(fileExplorer);
		delete = new DeleteAction(fileExplorer);
		properties = new PropertiesAction(fileExplorer);
		// ===============================================
		this.allActions = new Action[] { newFolder, newFile, open, edit, print, rename, delete, properties };
		this.zeroSelection = new Action[] { newFolder, newFile };
		this.singleSelection = allActions;
		this.multiSelection = new Action[] { newFolder, newFile, delete, properties };
	}

	public void show(Component invoker, int x, int y) {
		int count = this.fileExplorer.getSelectedItemsCount();
		if (0 == count)
			this.setupActions(this.zeroSelection);
		else if (1 == count)
			this.setupActions(this.singleSelection);
		else
			this.setupActions(this.multiSelection);
		super.show(invoker, x, y);
	}

	private void setupActions(Action[] actions) {
		for (int i = 0; i < allActions.length; i++) {
			Action a = allActions[i];
			if (true == this.hasReference(actions, a))
				a.setEnabled(true);
			else
				a.setEnabled(false);
		}
	}

	private boolean hasReference(Action[] array, Action a) {
		for (int i = 0; i < array.length; i++)
			if (a == array[i])
				return true;
		return false;
	}
}
